
#include <vector>
#include <string>
#include "fileList.h"
#include <iostream>
#include <vector>
#include <boost/filesystem.hpp>


tstring CFileList::output_path;

CFileList::CFileList(unsigned int nNumOfWorkers):m_fileList(new CVectorOfString),m_vecOfVec(new CVectorOfVector)
{
    m_nNumOfWorkers = nNumOfWorkers;
}


void CFileList::buildFilesList(const tstring& path)
{
    if (boost::filesystem::is_directory(path))
    {
        for (boost::filesystem::directory_iterator itr(path); itr!=boost::filesystem::directory_iterator(); ++itr)
        {
            if( itr->path().extension() == ".NEF" )
            {
               m_fileList->push_back(itr->path().root_path().string() + itr->path().relative_path().string()); 
               //std::cout<<itr->path().root_path().string()<<std::endl;
            }
        }
        int nCount = m_fileList->size()/m_nNumOfWorkers;
        int nRest = m_fileList->size()%m_nNumOfWorkers;
        printf("size = %i\tcount = %i \t rest = %i\n",static_cast<int>(m_fileList->size()),nCount,nRest);
        for( int i=0;i<(m_nNumOfWorkers);++i)
        {
            int nStart,nEnd;
            if(i!=(m_nNumOfWorkers-1))
            {
                nStart = (i*nCount);
                nEnd = nStart+nCount;
            }
            else
            {
                nStart = (i*nCount);
                nEnd = nStart+nCount + nRest;
            }
            CVectorOfString *tmp = new CVectorOfString((m_fileList->begin()+nStart),(m_fileList->begin()+nEnd));
            CPrtVectorOfString  partOfList(tmp); 
            m_vecOfVec->push_back(partOfList);
        }
        
    }
}

void CFileList::print() const
{
  //  CVectorOfVector::const_iterator it = m_fileList->begin();
  //  for(;it!=m_fileList->end();++it)
  //  {
  //      std::cout << *it <<std::endl;
  //  }
}

const CPrtVectorOfVector CFileList::getFileList() const
{
    return m_vecOfVec;
}

bool CFileList::createOutputFolder(const tstring& folder_path)
{
  if (!boost::filesystem::exists(folder_path)) // create folder
  {
      if(!boost::filesystem::create_directory(folder_path))
      {
          printf("Cant create folder %s\n",folder_path.c_str());
          return false;
      }
  }
  CFileList::output_path = folder_path;  
  return true;
}

tstring CFileList::getFileName(const tstring& path) 
{
    std::size_t nPos =  path.find_last_of("/\\");
    if(tstring::npos!=nPos)
    {
        return path.substr(nPos+1);
    }
    return tstring();
}

